<template>
  <div class="container">
    <!-- 头部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>内容</el-breadcrumb-item>
      <el-breadcrumb-item>内容列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-button type="primary" @click="$router.push('/home/content/add')">添加内容</el-button>
      <el-select v-model="cid" placeholder="全部分类" @change="handleCateChange" clearable>
        <el-option v-for="item in cateList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
      <el-table :data="contentList" border>
        <el-table-column type="index" label="#" width="80"></el-table-column>
        <el-table-column prop="cateName" label="分类" width="80"></el-table-column>
        <el-table-column label="图片" width="105">
          <template v-slot="scope">
            <img :src="baseURL + 'upload/' + scope.row.image" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column label="状态" width="80">
          <template v-slot="scope">
            {{ scope.row.status === 2 ? '推荐' : '默认' }}
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="180">
          <template v-slot="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              round
              @click="$router.push(`/home/content/edit/${scope.row.id}`)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              round
              @click="removeContentById(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分类区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 4, 8, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Content',
  data() {
    return {
      contentList: [],
      queryInfo: {
        pagenum: 1,
        pagesize: 4
      },
      total: 0,
      // 当前选中的分类id
      cid: '',
      // 分类列表
      cateList: []
    }
  },
  created() {
    this.getContentList()
    this.getCateList()
  },
  methods: {
    // 获取内容列表
    async getContentList() {
      const { data: res } = await this.$http.get(`/admin/content/${this.cid}`, {
        params: this.queryInfo
      })
      this.contentList = res.data
      this.total = res.total
    },
    // 监听显示条数的事件处理函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      // 重新获取数据
      this.getContentList()
    },
    // 监听当前页数的事件处理函数
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      // 重新获取数据
      this.getContentList()
    },
    // 根据id删除内容
    removeContentById(row) {
      this.$confirm('确定要删除内容？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`/admin/content/${row.id}`)
          if (res.status !== 200) return this.$message.error(res.msg)
          this.$message.success(res.msg)
          // 重新获取页面数据
          this.getContentList()
        })
        .catch(() => this.$message.info('已取消删除'))
    },
    // 获取所有分类列表
    getCateList() {
      this.$http.get('/home/cate').then(res => {
        this.cateList = res.data
      })
    },
    handleCateChange() {
      // 重置当前页数
      this.queryInfo.pagenum = 1
      // 重新获取数据
      this.getContentList()
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;

  img {
    width: 80px;
    height: 60px;
    object-fit: cover;
    vertical-align: middle;
  }
}
.el-select {
  margin-left: 20px;
}
</style>
